package purgehelper.custom.controls;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * PurgeTray sets up the image and listens for mouse movement on the tray icon.
 * Will handle the tray menu events such as clicking to open the program, when
 * it is hidden.
 * @author leadiv
 */
public class PurgeTray implements MouseListener, MouseMotionListener {
    public PurgeTray(Image icon, String tooltip) {
        trayIcon = SystemTray.isSupported() ?
            new TrayIcon(icon, tooltip) : null;

        this.init();
    }

    public PurgeTray(Image icon) {
        trayIcon = SystemTray.isSupported() ?
            new TrayIcon(icon) : null;
        
        this.init();
    }
    
    private void init() {
        if(SystemTray.isSupported()) {
            try {
                trayIcon.addMouseListener(this);
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.out.println("TrayIcon could not be added.");
                return;
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
        // do nothing for now
        System.out.println("Mouse Clicked");
    }

    public void mousePressed(MouseEvent e) {
        // do nothing for now
        System.out.println("Mouse pressed");
    }

    public void mouseReleased(MouseEvent e) {
        // do nothing for now
        System.out.println("Mouse released");
    }

    public void mouseEntered(MouseEvent e) {
        // does not work in Windows...
    }

    public void mouseExited(MouseEvent e) {
        // does not work in Windows...
    }

    public void mouseDragged(MouseEvent e) {
        // does not work in Windows...
    }

    public void mouseMoved(MouseEvent e) {
       // does not work in Windows...
    }

    private final SystemTray tray = SystemTray.isSupported() ? SystemTray.getSystemTray() : null;
    private final TrayIcon trayIcon;
}
